Imports Microsoft.VisualBasic

Public Class Utility


    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function GetKey(ByVal season As String, ByVal week As String, ByVal player As Base) As Lookup
        Dim key As New Lookup

        key.season = season
        key.week = week
        key.id = player.ID
        Return key
    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function GetBase(ByVal col As Collection, ByVal id As Integer) As Base

        For Each _base As Base In col

            If (_base.ID = id) Then
                Return _base
            End If
        Next
        Return Nothing
    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function GetBytes(ByVal fileName As String) As Byte()

        Dim fs As New IO.FileStream(fileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)

        Dim size As Integer = Convert.ToInt32(fs.Length.ToString())

        Dim content(size) As Byte

        Dim i As Integer = fs.Read(content, 0, size)

        fs.Flush()
        fs.Close()
        fs.Dispose()
        fs = Nothing

        Return content
    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Private Shared Function GetSerFileName(ByVal appData As String, ByVal rId As Integer) As String

        Dim saveString As String = appData & "Downloads\" & "sample-template-with-data-" & rId & ".ser"


        Return saveString

    End Function

    ''' <summary>
    ''' write the object to a file
    ''' read that file in as bytes and save to db
    ''' delete the file
    ''' </summary>
    ''' <param name="appData"></param>
    ''' <param name="rId"></param>
    ''' <param name="eng"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function Serialize(ByVal appData As String, ByVal rId As Integer, ByVal eng As PrivateEngine) As Boolean

        Try
            Dim filename As String = GetSerFileName(appData, rId)

            Dim stream As New IO.StreamWriter(filename, False)
            Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            bf.Serialize(stream.BaseStream, eng)
            stream.Close()

            Dim fileData() As Byte = Utility.GetBytes(filename)

            'insert entry in report table 'success', data, timestamp
            DBConnection.UPD_REPORT_ENTRY_SUCCESS(rId, fileData)

            IO.File.Delete(filename)
            Return True
        Catch ex As Exception
            Return False
        End Try



    End Function

    ''' <summary>
    ''' write bytes to a file
    ''' read that file in as an object and deserialise it
    ''' delete the file
    ''' </summary>
    ''' <param name="appData"></param>
    ''' <param name="rId"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function DeSerialize(ByVal appData As String, ByVal rId As Integer)

        Try
            Dim rpt As Reporting = DBConnection.FET_REPORT(rId)
            Dim filename As String = GetSerFileName(appData, rId)

            Dim str As New IO.FileStream(filename, IO.FileMode.Create, IO.FileAccess.ReadWrite, IO.FileShare.ReadWrite)
            str.Write(rpt.ReportData, 0, rpt.ReportData.Length)

            str.Close()
            str.Dispose()

            Dim stream As New IO.StreamReader(filename)

            Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            Dim eng As PrivateEngine = CType(bf.Deserialize(stream.BaseStream), PrivateEngine)
            stream.Close()
            stream.Dispose()

            IO.File.Delete(filename)

            Return eng
        Catch ex As Exception
            Return Nothing
        End Try


    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function SendEmail(ByVal att As String, ByVal emailId As Integer) As Boolean
        Try
            '!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS
            Const ToAddress As String = "har4winn@yahoo.com"
            Const FromAddress As String = "har4winnn@gmail.com"

            '(1) Create the MailMessage instance
            Dim mm As New Net.Mail.MailMessage(FromAddress, ToAddress)

            '(2) Assign the MailMessage's properties
            mm.Subject = "Email#: " & emailId
            mm.Body = "See attachment for report."
            mm.IsBodyHtml = False

            '(3) Create the SmtpClient object
            Dim smtp As New Net.Mail.SmtpClient
            Dim attachment As New Net.Mail.Attachment(att) 'create the attachment
            mm.Attachments.Add(attachment) 'add the attachment

            '(4) Send the MailMessage (will use the Web.config settings)
            smtp.Send(mm)
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

End Class
